package core

import (
	"github.com/go-playground/locales/zh"

	"github.com/gin-gonic/gin/binding"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	translation "github.com/go-playground/validator/v10/translations/zh"
)

var (
	trans ut.Translator
)

//初始化
func ValidatorTranslatorInit() {
	trans, _ = ut.New(zh.New()).GetTranslator("zh")

	translation.RegisterDefaultTranslations(binding.Validator.Engine().(*validator.Validate), trans)
}

//返回错误信息
func ValidatorTranslateError(err error) (ret string) {
	if validationErrors, ok := err.(validator.ValidationErrors); !ok {
		return err.Error()
	} else {
		for _, e := range validationErrors {
			ret += e.Translate(trans) + ";"
		}
	}
	return ret
}
